package com.sk.cardeal.act.home.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sk.cardeal.R;
import com.sk.cardeal.adapter.BrandAdapter;
import com.sk.cardeal.adapter.HotBrandAdapter;
import com.sk.cardeal.base.BaseFragment;
import com.sk.cardeal.callback.MyCallBack;
import com.sk.cardeal.entity.Brand;
import com.sk.cardeal.view.SideBar;
import com.sk.cardeal.view.TitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by sk on 2018/1/11.
 */

public class ChooseBrandFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.title_view)
    TitleView titleView;
    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;
    @BindView(R.id.side_bar)
    SideBar sideBar;
    Unbinder unbinder;
    @BindView(R.id.dialog)
    TextView dialog;
    Unbinder unbinder1;
    @BindView(R.id.hot_ryc)
    RecyclerView hotRyc;

    private List<Brand> allDataList = new ArrayList<>();
    private List<Brand> hotDataList = new ArrayList<>();

    private BrandAdapter adapter;
    private HotBrandAdapter hotBrandAdapter;

    private MyCallBack myCallBack;

    public MyCallBack getMyCallBack() {
        return myCallBack;
    }

    public void setMyCallBack(MyCallBack myCallBack) {
        this.myCallBack = myCallBack;
    }

    @Override
    protected int getContentView() {
        return R.layout.fragment_choose_brand;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        titleView.setMyCallBack(this);

        dataRyc.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new BrandAdapter(R.layout.item_brand, allDataList);
        dataRyc.setAdapter(adapter);
        adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
        adapter.setOnItemClickListener(this);
        //设置右侧[A-Z]快速导航栏触摸监听
        sideBar.setTextView(dialog);
        sideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {

            @Override
            public void onTouchingLetterChanged(String s) {
                //该字母首次出现的位置
                int position = adapter.getPositionForSection(s);
                if (position != -1) {
                    dataRyc.scrollToPosition(position);
                }
            }
        });

        hotRyc.setLayoutManager(new GridLayoutManager(mContext,6));
        hotBrandAdapter = new HotBrandAdapter(R.layout.item_hot_brand,hotDataList);
        hotRyc.setAdapter(hotBrandAdapter);
        hotBrandAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                myCallBack.selectBrand(hotDataList.get(position));
            }
        });
    }

    public void setData(List<Brand> brandList) {
        allDataList.clear();
        allDataList.addAll(brandList);
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }

        final List<Brand> hotList = new ArrayList<>();
        for(Brand brand : brandList){
            if(brand.getIsHot() == 1){
                hotList.add(brand);
            }
        }

        hotDataList.clear();
        hotDataList.addAll(hotList);

        if(hotBrandAdapter!=null){
            hotBrandAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void clickTitle(int i) {
        super.clickTitle(i);
        switch (i) {
            case 1:
                getActivity().finish();
                break;
            case 2:
                break;
            case 3:
                break;
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        myCallBack.selectBrand(allDataList.get(position));
    }

}
